package com.product.shop.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.product.shop.util.OssUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.product.shop.entity.GoodsEntity;
import com.product.shop.service.GoodsService;
import com.product.common.utils.PageUtils;
import com.product.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 
 *
 * @author brh
 * @email sunlightcs@gmail.com
 * @date 2022-02-22 16:30:11
 */
@RestController
@RequestMapping("shop/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private OssUtils ossUtils;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = goodsService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		GoodsEntity goods = goodsService.getById(id);

        return R.ok().put("goods", goods);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody GoodsEntity goods){
		goodsService.save(goods);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody GoodsEntity goods){
		goodsService.updateById(goods);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		goodsService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    //上传图片
    @RequestMapping("/upload")
    public R upload(MultipartFile file){
        R r= ossUtils.uploadFile(file);
        return r;
    }

    //商品下拉
    @RequestMapping("/shopXl")
    public R shopXl(){
       List<GoodsEntity> goods= goodsService.shopXl();
       return R.ok().put("goodsName",goods);
    }

    @RequestMapping("/goodsHx")
    public R goodsHx(@RequestParam String goodsName){
        GoodsEntity goods = goodsService.goodsHx(goodsName);
        return R.ok().put("goods",goods);
    }

}
